﻿using BW.WMS.WareHouseCheck.DTO;
using BW.WMS.WareHouseStatistics.Page;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using BW.WMS.WareHouseAllot;
using BW.WMS.WareHouseManage;

namespace BW.WMS.WareHouseCheck
{
    /// <summary>
    /// 保质期查询接口实现
    /// </summary>
    public class ExpirationDateService : ApplicationService, IExpirationDateService
    {
        private readonly IRepository<Inventory> _inventoryRepository;
        private readonly IRepository<GoodsModel> _goodsRepository;
        private readonly IRepository<GoodsTypeModel> _goodsTypeRepository;
        private readonly IRepository<Warehouse> _warehouseRepository;
        private readonly IRepository<Zone> _zoneRepository;

        public ExpirationDateService(
            IRepository<Inventory> inventoryRepository,
            IRepository<GoodsModel> goodsRepository,
            IRepository<GoodsTypeModel> goodsTypeRepository,
            IRepository<Warehouse> warehouseRepository,
            IRepository<Zone> zoneRepository)
        {
            _inventoryRepository = inventoryRepository;
            _goodsRepository = goodsRepository;
            _goodsTypeRepository = goodsTypeRepository;
            _warehouseRepository = warehouseRepository;
            _zoneRepository = zoneRepository;
        }

        public async Task<PagedResultDto<ExpirationDateDTO>> GetStockInquiryPageList(PageExpirationDate page)
        {
            var inventoryRepository = await _inventoryRepository.GetQueryableAsync();
            var goodsRepository = await _goodsRepository.GetQueryableAsync();
            var goodsTypeRepository = await _goodsTypeRepository.GetQueryableAsync();
            var warehouseRepository = await _warehouseRepository.GetQueryableAsync();
            var zoneRepository = await _zoneRepository.GetQueryableAsync();

            // 构建多表联查查询
            var query = from inventory in inventoryRepository
                        join goods in goodsRepository on inventory.MaterialId equals goods.Id
                       join goodsType in goodsTypeRepository on goods.GoodsTypeId equals goodsType.Id
                       join warehouse in warehouseRepository on inventory.WarehouseId equals warehouse.Id
                       join zone in zoneRepository on inventory.ZoneId equals zone.Id
                       where goods.GoodsStatus // 只查询启用状态的商品
                       select new ExpirationDateDTO
                       {
                           Id = inventory.Id,
                           GoodsCode = goods.GoodsCode,
                           GoodsName = goods.GoodsName,
                           GoodsStatus = goods.GoodsStatus,
                           GoodsTypeId = goods.GoodsTypeId,
                           GoodsTypeName = goodsType.GoodsTypeName,
                           GoodsUnit = goods.GoodsUnit,
                           Name = warehouse.Name, // 仓库名称
                           Name1 = zone.Name, // 库区名称
                           Inventory = (int)inventory.Quantity, // 库存数量
                           Sort = goods.Sort,
                           Lifetime = goods.Lifetime,
                           // 计算剩余天数：过期日期 - 当前日期
                           RemainingDays = inventory.ExpiryDate.HasValue 
                               ? (int)(inventory.ExpiryDate.Value - DateTime.Now).TotalDays 
                               : 0,
                           // 预警天数
                           WarningDays = goods.EarlyWarningDays,
                           CreationTime = inventory.CreationTime,
                           CreatorId = inventory.CreatorId,
                           LastModificationTime = inventory.LastModificationTime,
                           LastModifierId = inventory.LastModifierId
                       };

            // 应用过滤条件
            if (!string.IsNullOrWhiteSpace(page.GoodsName))
            {
                query = query.Where(x => x.GoodsName.Contains(page.GoodsName));
            }

            if (page.Lifetime!=null)
            {
                query = query.Where(x => x.Lifetime.Contains(page.Lifetime));
            }

            // 获取总记录数
            var totalCount = await query.CountAsync();

            // 应用分页
            var items = await query
                .Skip((page.PageIndex - 1) * page.PageSize)
                .Take(page.PageSize)
                .ToListAsync();

            return new PagedResultDto<ExpirationDateDTO>
            {
                TotalCount = totalCount,
                Items = items
            };
        }
    }
}
